<?php

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
	/**
	 * Setup errorcontroller
	 */
	protected function _initError()
    {
     	$front = $this->bootstrap('frontController')->frontController;
        $front->throwExceptions(false);
        $front->registerPlugin(new Zend_Controller_Plugin_ErrorHandler(array(
            'module'     => 'error',
            'controller' => 'error',
            'action'     => 'error'
        )));
    }
	
    /**
     * Setup autoloading
     */
	protected function _initAutoload()
	{
		//setup namespace loaders
		$loader = Zend_Loader_Autoloader::getInstance();
		$loader->setFallbackAutoloader(true);
		$loader->suppressNotFoundWarnings(false);
		$loader->registerNamespace('Source');
		$loader->registerNamespace('Zym');
		$loader->registerNamespace('ZFDebug');
		
		// setup module resource loaders
		$front   = $this->bootstrap('frontController')->frontController;
		$modules = $front->getControllerDirectory();
		foreach(array_keys($modules) as $module){
			$module = strtolower($module);
			$loader = new Zend_Application_Module_Autoloader(array(
				'namespace' => ucfirst($module) . '_',
				'basePath'  => APPLICATION_PATH . '/modules/' . $module
			));
		}
	}

	protected function _initCache()
	{
		$frontendOptions = array('lifetime' => 7200, 'automatic_serialization' => true);
		$backendOptions = array('cache_dir' => DATA_PATH . '/cache');
		$cache = Zend_Cache::factory('Core', 'File', $frontendOptions, $backendOptions);
		Zend_Registry::set('cache', $cache);
		return $cache;
		
	}
	/**
	 * Setup config
	 */
	protected function _initConfig()
	{
		$this->bootstrap('cache');
		$cache = $this->getResource('cache');
		
		if($cache->test('source_config')){
			$config = $cache->load('source_config');
		} else {
		
			// add application config
			$appConfig = new Zend_Config($this->getOptions(),true);
			Source_Config::add($appConfig);
			
			// add source config
			$sourceFile = APPLICATION_PATH . '/configs/source.xml';
			$sourceConfig = new Zend_Config_Xml($sourceFile,APPLICATION_ENV,true);		
			Source_Config::add($sourceConfig);
			
			// add modules config
			$front 		= $this->bootstrap('frontController')->frontController;
			$modules	= $front->getControllerDirectory();
			foreach(array_keys($modules) as $module){
				$module = strtolower($module);
				$moduleConfigPath = APPLICATION_PATH . '/modules/' . $module . '/configs/';
				$moduleConfigFile = $moduleConfigPath . 'module.xml';
				$moduleConfig = new Zend_Config_Xml($moduleConfigFile,APPLICATION_ENV,true);
				Source_Config::add($moduleConfig);
			}		
			
			// return config
			$config = Source_Config::getInstance();
			
			// save cache
			$cache->save($config,'source_config',array('config'));
		}
		
		return $config;
	}
	
	/**
	 * Setup view
	 */
	protected function _initView()
	{
		$this->bootstrap('config');
		$config = $this->getResource('config');
		
		$view = new Source_View();
		$view->headTitle($config->getData('design/application/title'))
			 ->setSeparator($config->getData('design/application/title_separator'));
		$view->doctype($config->getData('design/application/doctype'));	 
		
		$viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('ViewRenderer');
		$viewRenderer->view = $view;
		Zend_Registry::set('source_view',$view);
		return $view;
	}
}